package org.dj.eoms.system.web.controller;


import org.dj.eoms.system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by DengJun on 16/1/15.
 */

@Controller
@RequestMapping({"/system/loginController/"})
public class LoginController {

    private String succeedUrl;

    public LoginController() {

        this.succeedUrl = "/system/mainController/main.do";
    }

    @Autowired
    protected LoginService loginService;


    @RequestMapping(value = "login")
    public void login(HttpServletRequest req,
                      HttpServletResponse res,
                      @RequestParam("username") String username,
                      @RequestParam("password") String password) throws Exception {
        try{
            loginService.loginByAccountAndPwd(username,password);
            res.sendRedirect(req.getContextPath() + this.succeedUrl);
        }catch (Exception e){
            String errorMessage = username + " : " + e.getMessage();
            res.sendRedirect(req.getContextPath() + "/system/loginController/loginError.do?message=" + errorMessage);
        }


    }

    @RequestMapping(value = "loginError")
    public String  loginError(HttpServletRequest req,
                      HttpServletResponse res,
                      @RequestParam("message") String message) {

            req.getSession().setAttribute("SPRING_SECURITY_LAST_EXCEPTION",
                    message);
            return "login";



    }


}
